package com.hmall.controller;

import com.hdmall.business.common.annotation.LoginRequired;
import com.hdmall.business.common.common.ServerResponse;
import com.hmall.model.SysUser;
import com.hmall.service.MallProductPlanService;
import com.hmall.util.TokenUtil;
import com.hmall.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import redis.clients.jedis.JedisPool;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @Author zhoutao
 */
@RestController
@RequestMapping("/plan/")
@Api(value = "生产计划API", description = "生产计划接口")
public class ProductPlanController {

    private final JedisPool jedisPool;

    TokenUtil tokenUtil = new TokenUtil();

    private final MallProductPlanService mallProductPlanService;

    @Autowired
    public ProductPlanController(JedisPool jedisPool, MallProductPlanService mallProductPlanService) {
        this.jedisPool = jedisPool;
        this.mallProductPlanService = mallProductPlanService;
    }


    /**
     * @Date 2018/11/9 14:45
     * @Param [token]
     * @return com.hmall.common.ServerResponse
     * @Description // 列出所有业务员
     */
    @LoginRequired
    @GetMapping("listAllSalesMan")
    @ApiOperation(value = "列出所有某个与用户绑定的办事处的业务员")
    public ServerResponse listAllSalesMan(String token) throws Exception {

        SysUser user = tokenUtil.tokenToUser(jedisPool, token).getData();
        return mallProductPlanService.listAllSalesMan(user.getUserId());
    }


    /**
     * @Date 2018/11/9 14:46
     * @Param [token, productListJsonStr, planId, remark, submitPerson]
     * @return com.hmall.common.ServerResponse
     * @Description //
     *     创建生产计划，一个月只能创建一次 24号 00:00:00
     *     默认创建人为用户
     */
    @LoginRequired
    @PostMapping("createProductPlan")
    @ApiOperation(value = "用户创建生产计划",notes = "其实已经有模板了，只是作更新操作, 如果不传递planId默认当月的")
    public ServerResponse createProductPlan(String token, String productListJsonStr, Integer planId, String remark, @RequestParam(value = "submitPerson", defaultValue = "1") Integer submitPerson) throws Exception {
        
        SysUser user = tokenUtil.tokenToUser(jedisPool, token).getData();

        return mallProductPlanService.createProductPlanByUser(user.getUserId(), productListJsonStr, planId, remark, submitPerson);
    }



    /**
     * @Date 2018/11/9 14:46
     * @Param [token, status, pageSize, pageNum]
     * @return com.hmall.common.ServerResponse<com.hmall.vo.PageVo>
     * @Description //
     * 列出个人的生产计划
     *     全部，审核中，审核通过，审核未通过
     *     1, 2, 3, 4
     */
    @LoginRequired
    @GetMapping("listProductPlan")
    @ApiOperation(value = "列出生产计划", notes = "对于传进来的status 1 全部 2 审核中 3.通过 4.不通过 5.未审核 6.已失效——————对于具体某个计划的状态: //0 未审核  1 正在审核处理  2 审核通过 3 未通过 4.已失效" )
    public ServerResponse<PageVo<MallProductPlanVo>> listProductPlan(String token, Integer status, @RequestParam(value = "pageSize", defaultValue = "4") Integer pageSize, @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) throws Exception {

        
        SysUser user = tokenUtil.tokenToUser(jedisPool, token).getData();
        return mallProductPlanService.listProductPlan(user.getUserId(), status, pageNum, pageSize);
    }


    /**
     * @Date 2018/11/9 14:46
     * @Param [planId]
     * @return com.hmall.common.ServerResponse<com.hmall.vo.MallProductPlanDetialListVo>
     * @Description //
     * 将来优化只能看自己的planId
     *     优化加入token
     *     列出生产计划详情
     */
    @GetMapping("listProductPlanDetial")
    @ApiOperation(value = "列出生产计划详情")
    public ServerResponse<MallProductPlanDetialListVo> listProductPlanDetail(int planId) throws Exception {

        return mallProductPlanService.listProductPlanDetial(planId);
    }


    /**
     * @Date 2018/11/9 14:46
     * @Param [token, planId]
     * @return com.hmall.common.ServerResponse
     * @Description //
     * 列出当前用户的本月的未审核的生产计划
     */
    @LoginRequired
    @GetMapping("getProductPlanDetial")
    @ApiOperation(value = "当前用户未审核通过的生产计划详情,支持自搜")
    public ServerResponse getProductPlanDetail(String token, Integer planId) throws Exception {

        //如果这个计划不是这个用户的无法查询出来
        SysUser user = tokenUtil.tokenToUser(jedisPool, token).getData();
        return mallProductPlanService.getProductPlanDetail(user.getUserId(), planId);
    }
    @LoginRequired
    @GetMapping("checkDoPlan")
    @ApiOperation(value = "判断当前用户是否可以在当月下生产计划")
    public ServerResponse checkDoPlan(String token) throws Exception {

        SysUser user = tokenUtil.tokenToUser(jedisPool, token).getData();
        return mallProductPlanService.checkDoPlan(user.getUserId());
    }



    /**
     * @Date 2018/11/9 14:47
     * @Param [subModel, shopId]
     * @return com.hmall.common.ServerResponse
     * @Description //
     * 根据批号模糊匹配批号集合
     * shopId输入-1表示搜全部 未实现 写一个另外的查询，懒得写
     */
    @GetMapping("getModelList")
    @ApiOperation(value = "模糊匹配批号", notes = "上限15条,默认搜全部 华鼎为1")
    public ServerResponse getModelLikeList(String subModel, @RequestParam(value = "shopId", defaultValue = "-1") Integer shopId){

        int searCount = 15;
        return mallProductPlanService.getModelListList(subModel, shopId, searCount);
    }

    /**
     * @Date 2018/11/9 14:47
     * @Param [model]
     * @return com.hmall.common.ServerResponse<com.hmall.vo.ProductAndGradeVo>
     * @Description //
     * 根据产品的批号查询该产品的信息
     * ios端
     */
    @GetMapping("getInformationByModel")
    @ApiOperation(value = "根据产品的批号查询产品的信息")
    public ServerResponse<ProductAndGradeVo> getProductInformationByModel(String model){

        return mallProductPlanService.getProductInformationByModel(model);
    }


    /**
     * @Date 2018/11/9 14:48
     * @Param [jsonStr, remark]
     * @return com.hmall.common.ServerResponse<java.lang.String>
     * @Description // 打印预览
     */
    @PostMapping("printPlanOrder")
    @ApiOperation(value = "打印采购单")
    public ServerResponse<String> printPlanOrder(String jsonStr, String remark){

        return mallProductPlanService.printPlanOrder(jsonStr, remark);
    }

    /**
     * @Date 2018/11/9 14:48
     * @Param [token, keywords]
     * @return com.hmall.common.ServerResponse<java.util.List<com.hmall.vo.MallProductPlanVo>>
     * @Description //
     * PC端计划任务搜索
     */
    @LoginRequired
    @Deprecated
    @PostMapping("productPlanSearch")
    @ApiOperation(value = "PC端计划任务搜索")
    public ServerResponse<List<MallProductPlanVo>> productPlanSearch(String token, String keywords) throws Exception {

        
        SysUser user = tokenUtil.tokenToUser(jedisPool, token).getData();
        
        return mallProductPlanService.productPlanSearch(user.getUserId(), keywords);
    }

    /**
     * @Date 2018/11/9 14:48
     * @Param [token, customerUserId, month]
     * @return com.hmall.common.ServerResponse
     * @Description //
     * 用户列出上个月完成的生产计划
     */
    @LoginRequired
    @GetMapping("getLastMonthProductPlan")
    @ApiOperation(value = "获得上个月完成的生产计划", notes = "month不传默认上个月，如当前为8月，默认传7。2018/9/13 1.为测试展示上个月通过的和这个月通过的生产计划 2.手机办事处端也需要这个功能，传customerUserId")
    public ServerResponse getLastMonthProductPlan(String token, Integer customerUserId, Integer month) throws Exception {

        SysUser user = tokenUtil.tokenToUser(jedisPool, token).getData();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        if(null == month){
            month = calendar.get(Calendar.MONTH);
            //当前9月 month = 8 找8月1号到9月1号的生产计划
        }
        Integer year = calendar.get(Calendar.YEAR);

        int userId = user.getUserId();
        if(null != customerUserId && !"".equals(customerUserId)) {
            userId = customerUserId;
        }
        return mallProductPlanService.getLastMonthProductPlan(userId, year, month);
    }

    /**
     * @Date 2019/1/10 13:15
     * @Param [] 
     * @return com.hmall.common.ServerResponse
     * @Description 定时任务创建所有公司一年的计划
     */
    @GetMapping("createYearPlan")
    @ApiOperation(value = "创建所有公司一年的计划")
    public ServerResponse createYearPlan(){

        return mallProductPlanService.createYearPlan();
    }



}
